function reloads() {
    document.getElementById("codeImg").src="codeUtil?time=" + new Date();
}

function checkUserName() {
    if($("#name").val() == null || $("#name").val() === ""){
        $("#userName").text("用户名不能为空！");
        $("#userName").css("color","#ff0000");
        $("#btnSend").attr("disabled",true);
        flag = false
    }else{
        $.ajax({
            type:"POST",
            url:"checkUserNameServlet",
            contentType:"application/json; charset=utf-8",
            data:JSON.stringify({"userName":$("#name").val()}),
            dataType:"json",
            complete:function (data) {
                var json = eval("(" + data.responseText + ")");
                if(json.result === "false"){
                    $("#userName").text("用户名已经存在！");
                    $("#btnSend").attr("disabled",true);
                    flag = false
                }else if(json.result === "true"){
                    $("#userName").text("用户名可以使用！");
                    $("#btnSend").attr("disabled",false);
                    $("#userName").css("color","#00FF00");
                }
            }
        })
    }
}
function pwd1() {
    if($("#password").val() == null || $("#password").val() === ""){
        $("#pwd").text("密码不能为空！");
        $("#btnSend").attr("disabled",true);
    }else{
        $("#pwd").text("");
        $("#btnSend").attr("disabled",false);
    }
}
function checkEmailFormat() {
    if($("#email").val()=="") {
        $("#emailSpan").text("邮箱不能为空");
        $("#btnSend").attr("disabled",true);
        $("#email").focus();
    }
    var email=$("#email").val();
    if(!email.match(/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/)) {
        $("#emailSpan").text("格式不正确！请重新输入");
        $("#btnSend").attr("disabled",true);
        $("#email").focus();
    }else{
        $("#emailSpan").text("邮箱可以使用！");
        $("#emailSpan").css("color","#00FF00");
        $("#btnSend").attr("disabled",false);
    }
}
function checkPassword() {
    if($("#password").val() == null || $("#password").val() === ""){
        $("#pwd").text("密码不能为空！");
    }else{
        if($("#password").val() === $("#password2").val()){
            $("#pwd2").text("ok!");
            $("#btnSend").attr("disabled",false);
            $("#pwd2").css("color","#00FF00");
        }else {
            $("#pwd2").text("两次输入密码不正确！");
            $("#btnSend").attr("disabled",true);
        }
    }
}
var agree = document.getElementById("agree");
var btnSend = document.getElementById("btnSend");
var flag = false;
agree.onclick = function () {
    if(btnSend.disabled == ""){
        btnSend.disabled = "true";
        flag = false;
    }else{
        btnSend.disabled = "";
        flag = true;
    }
}
$("#btnSend").click(function () {
    $.ajax({
        type:"POST",
        url:"userRegisterServlet",
        contentType:"application/json; charset=utf-8",
        data:JSON.stringify(GetJsonData()),
        dataType:"json",
        complete:function (data) {
            var json = eval("("+data.responseText+")");
            if(json.result === "true"){
                window.location.href = json.url;
            }else if(json.result == "false"){
                alert("注册失败！请重新注册！");
                location.reload();
            }else if(json.code === "true"){
                $("#input_code").text("验证码错误！");
                reloads();
            }
        }
    })
});
function GetJsonData() {
    var json = {
        "loginName": $("#name").val(),
        "pwd": $("#password").val(),
        "email":$("#email").val(),
        "code":$("#code").val()
    };
    return json;
}